<template>
  <div class="app-container">
    <el-form ref="hospitalSet" :model="hospitalSet" label-width="80px">
      <el-form-item label="医院名称" :rules="[
      { required: true, message: '请输入医院名称', trigger: 'blur' }]"
      >
        <el-input v-model="hospitalSet.hosname"></el-input>
      </el-form-item>
      <el-form-item label="医院编号" :rules="[
      { required: true, message: '请输入医院编号', trigger: 'blur' }]"
      >
        <el-input v-model="hospitalSet.hoscode"></el-input>
      </el-form-item>
      <el-form-item label="api基础路径">
        <el-input v-model="hospitalSet.apiUrl"></el-input>
      </el-form-item>
      <el-form-item label="联系人姓名">
        <el-input v-model="hospitalSet.contactsName"></el-input>
      </el-form-item>
      <el-form-item label="联系人升级">
        <el-input v-model="hospitalSet.contactsPhone"></el-input>
      </el-form-item>
      <el-form-item>
      <el-button type="primary" @click="onSubmit('hospitalSet')">保存</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import hospitalSet from '@/api/hosp/hospitalSet'

export default {
  data() {
    return {
      hospitalSet:{
        id:'',
        hosname:'',
        hoscode:'',
        apiUrl:'',
        contactsName:'',
        contactsPhone:'',
      }

    }
  },
  created() {
    if(this.$route.params && this.$route.params.id){//如果路由有参数且参数中包含id，则做回显
      this.getHospitalSet(this.$route.params.id)
    }else{
      this.hospitalSet={}//如果id为空则清空，防止从edit页面切换到add时路由参数id已去除，但组件不刷新
    }
  },
  methods: {
    onSubmit(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
         if(!this.hospitalSet.id){//如果有id则插入，否则为修改
           this.saveHospitalSet()
         }else{
           this.updateHospitalSet()
         }
        } else {
          this.$message.erro("请按填写必填项")
        }
      });
    },
    getHospitalSet(id){
      hospitalSet.getHospitalSet(id)
      .then(response=>{
        this.hospitalSet=response.data
        console.log(this.hospitalSet.id)
      })
      .catch(error=>{
        console.log(error)
      })
    },
    saveHospitalSet(){
      hospitalSet.saveHospitalSet(this.hospitalSet)
      .then(response =>{
        alert(response.message);
      })
      .catch(error => {
        console.log(error)
      })
    },
    updateHospitalSet(){
      hospitalSet.updateHospitalSet(this.hospitalSet)
      .then(response =>{
        alert(response.message);
      })
      .catch(error => {
        console.log(error)
      })
    }
  }
}
</script>
